package com.wisezone.studentmanager.controller;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.wisezone.studentmanager.model.User;
import com.wisezone.studentmanager.server.UserServer;
import com.wisezone.studentmanager.utils.WebContext;

/**
 * 自动登录 扩展知识 1.如何清除cookie 2.代码封装 3.学习画流程图
 * 
 *
 * @author zhousir
 * @创建时间 2017年10月18日
 * 
 * login.servlet 
 */
public class LoginServlet extends HttpServlet {
	private static Logger logger = LogManager.getLogger(LoginServlet.class);
	
	private UserServer userServer;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		WebContext.foward("login/login", request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		WebApplicationContext webContext = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
		userServer = (UserServer)webContext.getBean("userServer");
		
		// 获取参数
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		String isAutoLogin = request.getParameter("loginAuto"); // 是否记住密码
		String code = request.getParameter("code"); // 验证码

		// 一、 验码不能为空
		if (code == null || "".equals(code.trim())) {
			request.setAttribute("message", "请输入验证码!");
			WebContext.foward("login/login", request, response);
			return;
		}

		// 二、 验证验证码
		HttpSession session = request.getSession();
		String codes = (String) session.getAttribute("code");
		if (!codes.equalsIgnoreCase(code)) {
			request.setAttribute("message", "验证码错误!");
			WebContext.foward("login/login", request, response);
			return;
		}

		// 三、验证用户名密码
		User user = userServer.login(name, password);
		if (user == null) {
			request.setAttribute("message", "用户名或密码出错!");
			WebContext.foward("login/login", request, response);
			return;
		}

		// 1.判断是否自动保存登录状态信息,实现记住密码功能
		if (isAutoLogin(isAutoLogin) == true) {
			doUserCookie(name, password, 60 * 60 * 24, response);
		} else {
			doUserCookie(null, null, 0, response);
		}
		// 2 保存登录状态到session,实现登录认证
		session.setAttribute("user", user);
		// 3. 进入主界面
		WebContext.foward("index", request, response);
	}

	/**
	 * 判断是否选中自动登录 true表示 : 自动登录 false表示: 不自动登录
	 * 
	 * @param isAutoLogin
	 * @return
	 */
	public boolean isAutoLogin(String isAutoLogin) {
		return isAutoLogin != null && !"null".equals(isAutoLogin);
	}

	/**
	 * 保存用户信息到cookie
	 */
	public void saveUserToCookie(String name, String password, HttpServletResponse response) {
		Cookie nameCookie = new Cookie("username", name);
		Cookie passwordCookie = new Cookie("password", password);
		nameCookie.setMaxAge(60 * 60 * 24);
		passwordCookie.setMaxAge(60 * 60 * 24);
		response.addCookie(nameCookie);
		response.addCookie(passwordCookie);
	}

	/**
	 * 清除保存用户状态信息的cookie
	 */
	public void clearUserFromCookie(HttpServletResponse response) {
		Cookie nameCookie = new Cookie("username", null);
		Cookie passwordCookie = new Cookie("password", null);
		nameCookie.setMaxAge(0);
		passwordCookie.setMaxAge(0);
		response.addCookie(nameCookie);
		response.addCookie(passwordCookie);
	}

	/**
	 * 处理用户登录状信息 保存登录状态或清除登录状态
	 */
	public void doUserCookie(String name, String password, int expiry, HttpServletResponse response) {
		Cookie nameCookie = new Cookie("username", name);
		Cookie passwordCookie = new Cookie("password", password);
		nameCookie.setMaxAge(expiry);
		passwordCookie.setMaxAge(expiry);
		response.addCookie(nameCookie);
		response.addCookie(passwordCookie);
	}

}
